package com.xiaodeng.xdblogspringboot.mapper;

import com.xiaodeng.xdblogspringboot.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

    //查询文章列表
    List<Article> articlelist(@Param("title") String title,@Param("categoryName") List<String> categoryName,@Param("size")Integer categoryNameSize);
    //  获取文章列表
    List<Article> articleList(@Param("title") String title,@Param("categoryName") List<String>  categoryName,@Param("size")Integer categoryNameSize);
    //新增文章
    @Insert("insert into article (title, content,cover_img,create_user, state,view_count,create_time, update_time) " +
            "values(#{article.title},#{article.content},#{article.coverImg},#{createId},#{article.state},#{article.viewCount},#{article.createTime},#{article.updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "article.id")
    void addarticle(@Param("article")  Article article, @Param("createId") Integer createId);

    void acRelation(@Param("articleId") Integer articleId, @Param("categoryId")List<String> categoryId);
    //根据文章ID单条查询
    @Select("select * from article a join user u on a.create_user = u.id  where a.id  = #{articleId}")
    Article getarticle(Integer articleId);
    //用户删除自己的文章
    @Delete("DELETE FROM article_category WHERE article_id = #{articleId}")
    void delACRelation(@Param("articleId") Integer articleId);

    @Delete("DELETE FROM article WHERE create_user = #{userId} AND id = #{articleId}")
    int delArticle(@Param("articleId") Integer articleId, @Param("userId") Integer userId);
    @Select("select view_count from article where id = #{articleId}")
    Integer getViewCount(Long articleId);
    //获取所有文章ID
    @Select("select id from article")
    List<Long> findAllarticlId();
    @Update("update article set view_count = #{viewCount} where id = #{articleId}")
    void updateViewCount(@Param("articleId") Long articleId,@Param("viewCount") Integer viewCount);
    @Select("select article.id,title,view_count from article join application a on article.id = a.article_id where state != '隐藏' and audit_status = '审核通过' order by view_count desc limit #{limit} ")
    List<Article> getHotArticles(int limit);
    /**
     * 更新文章信息
     * @param article 文章对象
     * @return 更新影响的行数
     */
    @Update("UPDATE article " +
            "SET title = #{article.title}, content = #{article.content}, " +
            "cover_img = #{article.coverImg}, state = #{article.state}, " +
            "update_time = #{article.updateTime} " +
            "WHERE id = #{article.id} AND create_user = #{userId}")
    int updateArticle(@Param("article") Article article, @Param("userId") Integer userId);
    /**
     * 删除文章与类别的关联关系
     * @param articleId 文章ID
     */
    @Delete("DELETE FROM article_category WHERE article_id = #{articleId}")
    void deleteArticleCategoryRelation(@Param("articleId") Integer articleId);

}
